package com.cote.douhahou.pretloan.msg;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.cote.douhahou.pretloan.beans.AppAkomapalendConfigCustomize;
import com.cote.douhahou.pretloan.beans.BaAkomapalendnkAccount;
import com.cote.douhahou.pretloan.network.HttpHelper;
import com.cote.douhahou.pretloan.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class DeleteAccountDiAkomapalendalog extends Dialog {

    private final FragmentActivity activity_akomapalend;

    private final BaAkomapalendnkAccount account_akomapalend;

    private final String page_akomapalend;

    private final Runnable dltActRespRunnable_akomapalend;

    public DeleteAccountDiAkomapalendalog(@NonNull FragmentActivity activity, BaAkomapalendnkAccount account, String page, Runnable dltActRespRunnable) {
        super(activity);
        this.activity_akomapalend = activity;
        this.account_akomapalend = account;
        this.page_akomapalend = page;
        this.dltActRespRunnable_akomapalend = dltActRespRunnable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_delete_account);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        initView_akomapalend();
    }

    @SuppressLint("SetTextI18n")
    private void initView_akomapalend(){

        TextView accountmobileTv  = findViewById(R.id.ddafa_tv_mobile);
        ImageView accountlogoIv = findViewById(R.id.ddafa_iv_logo);
        TextView accountchannelTv = findViewById(R.id.ddafa_tv_channel);

        //取消
        findViewById(R.id.ddafa_btn_cancel).setOnClickListener(v -> {
            HttpHelper.inst().submitEvent(page_akomapalend,"_DELETE_CANCEL_CLICK", account_akomapalend.getWalletMobile_akomapalend());
            dismiss();
        });

        //提交
        findViewById(R.id.ddafa_btn_submit).setOnClickListener(v -> {
            HttpHelper.inst().submitEvent(page_akomapalend,"_DELETE_CONFIRM_CLICK", account_akomapalend.getWalletMobile_akomapalend());
            HttpHelper.inst().removeAccount(activity_akomapalend, account_akomapalend.getId_akomapalend(),() -> {
                if (dltActRespRunnable_akomapalend !=null) dltActRespRunnable_akomapalend.run();
            });
            dismiss();
        });

        if (account_akomapalend != null){

            String channelDesc = account_akomapalend.getWalletDesc_akomapalend();

            accountmobileTv.setText(account_akomapalend.getWalletMobile_akomapalend());
            accountchannelTv.setText(channelDesc);

            //加载图标
            if (AppAkomapalendConfigCustomize.instance().getChannelLogoByDesc_akomapalend(channelDesc) == null){
                new Thread(() -> {
                    try {
                        if (TextUtils.isEmpty(account_akomapalend.getWalletLogo_akomapalend())) return;
                        if (!AppAkomapalendConfigCustomize.instance().hasAccountChannel_akomapalend()) return;
                        URL url = new URL(account_akomapalend.getWalletLogo_akomapalend());
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setConnectTimeout(5000);
                        connection.setRequestMethod("GET");
                        if (connection.getResponseCode()==200){
                            InputStream inputStream = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            AppAkomapalendConfigCustomize.instance().setChannelLogoByDesc_akomapalend(channelDesc,bitmap);
                            new Handler(Looper.getMainLooper()).post(() -> accountlogoIv.setImageBitmap(bitmap));
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }else accountlogoIv.setImageBitmap(AppAkomapalendConfigCustomize.instance().getChannelLogoByDesc_akomapalend(channelDesc));

        }

    }

}
